$(function(){
	var path="/card-manager";
	$(document).ready(function(){
		$("#showSellCoin").show();
		$("#showMoney").hide();
//		userId
		$.ajax({
	        type: "POST",
	        url: path+"/manager/agentQueryCurrentInfo.html",
	        dataType: 'json',
			contentType:'application/json',
			data: JSON.stringify({
				}),
	        success: function(json) {
	        	if(json.errorCode==undefined){
	        		return;
	        	}else if(json.errorCode>0){
	        		alertMsgs(json.errorMsg);
	        		return;
	        	}
	        	var obj = json.managerInfo;
	        	$("#userId").html(obj.id);
	        	$("#totalCoin").html(obj.agentCoin);
	        	
	        },
	        error: function(err) {
	            alertMsgs(err);
	        }
	  });
	   //showHistoryRefresh();
	});
	
	
	$(document).on("click", "#searchPlayer", function(e){ 
		var playerId = $("#playerId").val();
		if(playerId==""||playerId==undefined||playerId==null){
			alertMsgs("请输入玩家id");
			return;
		}
		$.ajax({
	        type: "POST",
	        url: path+ "/player/agentQueryPlayInfoById.html",
	        dataType: 'json',
			contentType:'application/json',
			data: JSON.stringify({
				"playerId":playerId
				}),
	        success: function(json) {
	        	if(json.errorCode==undefined){
	        		return;
	        	}else if(json.errorCode>0){
	        		alertMsgs(json.errorMsg);
	        		return;
	        	}
	        	var playerInfo = json.playerInfo;
	        	
	        	$("#playerIdTxt").html(playerInfo.playerId);
	        	$("#playerNickTxt").html(playerInfo.nickName);
	        	$("#playerCoinTxt").html(playerInfo.mahjongCoin);
	        	
	        },
	        error: function(err) {
	            alertMsgs(err);
	        }
	  });
	});
	
	
	$(document).on("click", "#sellCoinBtn", function(e){ 
		var playerId = $("#playerId").val();
		if(playerId==""||playerId==undefined||playerId==null){
			alertMsgs("请输入玩家id");
			return;
		}
		var coin = $("#sellCoinCount").val();
		if(validate(coin)){
			return;
		}
		$.ajax({
	        type: "POST",
	        url: path+ "/manager/agentSellCoin.html",
	        dataType: 'json',
			contentType:'application/json',
			data: JSON.stringify({
				"playerId":playerId,
				"coin":coin
				}),
	        success: function(json) {
	        	if(json.errorCode==undefined){
	        		return;
	        	}else if(json.errorCode>0){
	        		alertMsgs(json.errorMsg);
	        		return;
	        	}
	        	alertMsgs(json.result);
	        	//window.location.reload();
	        	//showHistoryRefresh();
	        },
	        error: function(err) {
	            alertMsgs(json.errorMsg);
	        }
	  });
	});
	//充值聚合支付平台
	$(document).on("click", "#rechargeCoinBtn", function(e){ 
		document.getElementById('a_form').submit();
//		var html='<html><head><meta http-equiv="Content-type" content="text/html; charset=utf-8">'+
//		'<script type="text/javascript" src="http://g.alicdn.com/sj/lib/jquery/dist/jquery.min.js"></script>'+ 
//		'<title>金吉麻将支付</title>'
//		'</head><body>hello</body></html>';
//		window.open("page.html", "newwindow").document.write(html);
		
		/*$.ajax({
	        type: "POST",
	        url: path+ "/pay/topay.html",
	        dataType: 'json',
			contentType:'application/json',
			data: JSON.stringify({
//				"playerId":playerId,
//				"coin":coin
				}),
	        success: function(json) {
	        	if(json.errorCode==undefined){
	        		return;
	        	}else if(json.errorCode>0){
	        		alertMsgs(json.errorMsg);
	        		return;
	        	}
	        	alertMsgs(json.result);
	        	window.location.reload();
	        },
	        error: function(err) {
	            alertMsgs(json.errorMsg);
	        }
	  });*/
	});
	
	
	
	$("#sell").click(function(){
		$("#showSellCoin").show();
		$("#showMoney").hide();
		
	})
	$("#recharge").click(function(){
		var strHtmlRech;
		$("#showSellCoin").hide();
		$("#showMoney").show();
		$.ajax({
	        type: "POST",
	        url: path+ "/trade/getTradeSellHistory.html",
	        dataType: 'json',
			contentType:'application/json',
			data: JSON.stringify({
				}),
	        success: function(json) {
	        	if(json.errorCode==undefined){
	        		return;
	        	}else if(json.errorCode>0){
	        		alertMsgs(json.errorMsg);
	        		return;
	        	}
	        	var list = json.tradeRecordList;
	        	$.each(list, function(index, element) {
	        		
	        		strHtmlRech=strHtmlRech+'<tr><td class="date">'+element.createDate+'</td>'+
	        		'<td class="money">'+element.orderMoney+'</td>'+
	        		'<td class="coin">'+element.tradeCoin+'</td></tr>';
	        	});
	        	
	        	$("#orderList").html(strHtmlRech);
	        	
	        },
	        error: function(err) {
	            alertMsgs(json.errorMsg);
	        }
	  });
		
	})
	
	/*function showHistoryRefresh(){
		var strHtml;
		$("#sellListTab").html("");
		  $.ajax({
		        type: "POST",
		        url: path+"/manager/agentQuerySellHistory.html",
		        dataType: 'json',
				contentType:'application/json',
				data: JSON.stringify({
					}),
		        success: function(json) {
		        	if(json.errorCode==undefined){
		        		return;
		        	}else if(json.errorCode>0){
		        		alertMsgs(json.errorMsg);
		        		return;
		        	}
		        	var list = json.sellHistoryList;
		        	
		        	$.each(list, function(index, element) {
		        		
		        		strHtml=strHtml+'<tr><td class="date">'+element.recordTime+'</td>'+
		        		'<td class="playerId">'+element.playerId+'</td>'+
		        		'<td class="playName">'+element.nickName+'</td>'+
		        		'<td class="coin">'+element.mahjongCoin+'</td></tr>';
		        	});
		        	
		        	$("#sellListTab").html(strHtml);
		        	
		        },
		        error: function(err) {
		            alertMsgs(json.errorMsg);
		        }
		  });
	}*/
	var wWidth = $(window).width();
	var dWidth = parseInt( wWidth - 100  );
	$("#editPassword").click(function(){
		var updateStr='<div class="page-container">  <div class="tableCon"> <div class="sui-row tableRow nopadding"> <div class="span2 text-r"> 新密码： </div> <div class="span3"> <input id="newPassword" type="text" placeholder="请输入新密码" class="input-text radius size-M" value=""> </div>  </div> <div class="sui-row tableRow nopadding"> <div class="span2 text-r"> 确认密码： </div> <div class="span3"> <input id="passwordSecond" type="text" placeholder="请输入密码" class="input-text radius size-M" value=""> </div>  </div> <div class="sui-row tableRow"> <div class="span3 text-r"> <a href="javascript:void(0);" class="sui-btn btn-xlarge btn-success" id="saveBtn">保存</a> <a href="javascript:void(0);" class="sui-btn btn-xlarge btn-primary" id="resetBtn">重置</a></div></div>  </div> </div>';
		//$('#detailModal').modal('shadeIn');
		$.alert({
			id:'updateModal',
			backdrop: true,//  决定是否为模态对话框添加一个背景遮罩层。另外，该属性指定static时，表示添加遮罩层，同时点击模态对话框的外部区域不会将其关闭。
			bgcolor: '#000',//   背景遮罩层颜色，默认是黑色。可以用rgba设置透明度
			keyboard: false,//   是否可由esc按键关闭
			title: '修改密码',
			body: updateStr,
			backdrop:false,
			closeBtn: true,   //是否显示右上角叉叉（html使用方式无需配置，直接改html结构即可））
			hasfoot: false,  //是否显示脚部  默认true
			width: dWidth,	 
			//height:mHeight,
			show:function(e){ },
			shown:function(e){},
			hide:function(e){
				//$('#detailModal').modal('shadeOut');
			} 
		})
	});
	$(document).on("click", "#resetBtn", function(e) {
		$("#newPassword").val("");
		$("#passwordSecond").val("");
	});

	$(document).on("click", "#saveBtn", function(e) {
		var newPassword = $("#newPassword").val();
		var passwordSecond = $("#passwordSecond").val();
		if(newPassword!=passwordSecond){
			alertMsgs("密码不一致");
			return;
		}
		$.ajax({
	        type: "POST",
	        url: path+"/agent/changePwd.html",
	        dataType: 'json',
			contentType:'application/json',
			data: JSON.stringify({
				"newPassword":newPassword
				}),
	        success: function(json) {
	        	if(json.errorCode==undefined){
	        		return;
	        	}else if(json.errorCode>0){
	        		alertMsgs(json.errorMessage);
	        		return;
	        	}
	        	alertMsgs("修改成功");
	        },
	        error: function(err) {
	            alertMsgs(err.responseText);
	        }
	        });
	});
})



 function validate(obj){
    var reg = new RegExp("^[0-9]*$");
    if(!reg.test(obj)){
        alert("请输入数字!");
        return true;
    }
    if(!/^[0-9]+$/.test(obj)){
        alert("请输入数字!");
        return true;
    }
    return false;
  }